import 'dart:convert';

import 'package:exams_app/net/Address.dart';
import 'package:exams_app/net/DioManager.dart';
import 'package:exams_app/net/persisten_storage.dart';
import 'package:exams_app/widget/hexcolor.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_html/flutter_html.dart';
import 'help_logic.dart';
import 'package:get/get.dart';

class HelpPages extends StatelessWidget {



  HelpLogic logic = Get.put(HelpLogic());


  @override
  Widget build(BuildContext context) {
    return Scaffold(

      backgroundColor: Colors.red,
        appBar: AppBar(
          title: Text('帮助中心'),
          centerTitle: true,
        ),
        body: GetBuilder<HelpLogic>(

          builder: (helpLogic) {
            return Container(
              color: HexColor('#F9F9FF'),
              child: EasyRefresh.custom(

                emptyWidget: helpLogic.emptyData(),
                controller: helpLogic.esController,
                header: MaterialHeader(),
                footer: MaterialFooter(),
                onLoad: helpLogic.onLoad,
                onRefresh: helpLogic.onRefresh,
                slivers: [
                  SliverList(
                      delegate: SliverChildBuilderDelegate(_buildListItem,
                          childCount: helpLogic.dataList.length)),
                ],
              ),
            );
          },
        ));
  }

  Widget _buildListItem(BuildContext context, int index) {

    Data model = logic.dataList[index];


    return Container(
      color: Colors.white,
      child: Column(
        children: [
          ListTile(
            title: Text('${model.name}',style: TextStyle(fontSize: 14,color: HexColor('#333333')),),
            trailing: Icon(Icons.arrow_forward_ios_sharp,size: 15,color: HexColor('#BBBBBB'),),
            onTap: (){
              Get.to(HelpDetailPage(model.id));
            },
          ),
          Divider(
            color: HexColor('#D8D8D8'),
          )
        ],
      ),
    );
    // return GestureDetector(
    //     onTap: () {
    //       Get.to(HelpDetailPage(model.id));
    //     },
    //     child: Container(
    //       color: Colors.white,
    //       margin: EdgeInsets.only(left: 0, top: 0),
    //       padding: EdgeInsets.all(15),
    //       child: Column(
    //         crossAxisAlignment: CrossAxisAlignment.start,
    //         children: [
    //           Text(
    //             '名称:${model.name}',
    //             style: TextStyle(fontSize: 14, color: HexColor('#333333')),
    //           ),
    //           SizedBox(
    //             height: 5,
    //           ),
    //           // Text('描述:${model.describe}'),
    //           SizedBox(
    //             height: 5,
    //           ),
    //           Text('创建时间:${model.createDate}'),
    //         ],
    //       ),
    //     ));
  }
}

class HelpDetailPage extends StatefulWidget {

  var help_id;

  HelpDetailPage(this.help_id);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return HelpDetailPageState();
  }
}

class HelpDetailPageState extends State<HelpDetailPage> {
  Map json;

  void requestData() async {
    int page = 0;

    var params = {
      'help_id': widget.help_id,
    };

    String result =
        await DioUtils(url: await PersistentStorage().getStorage('urlStr'))
            .postModel(Address.getattt,
                function_name: '_api_one', model: 'res.help', params: params);
    Map _json = jsonDecode(result);
    json = _json;
    setState(() {});
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    requestData();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('${json['data'][0]['name']}'),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            margin: EdgeInsets.all(15),
            child: json == null
                ? Text('')
                : Text(
                    '${json['data'][0]['name']}',
                    style: TextStyle(
                        fontSize: 18,
                        color: HexColor('#333333'),
                        fontWeight: FontWeight.w500),
                  ),
          ),
          Divider(
            indent: 15,
            endIndent: 15,
            color: HexColor('#D8D8D8'),
          ),
          Container(
              margin: EdgeInsets.only(left: 10),
              child: Html(
                data:json==null?'':json['data'][0]['describe'],
              ))
        ],
      ),
    );
  }
}
